package com.showcai.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.showcai.dao.ApplyMaintainMapper;
import com.showcai.dao.PrizesMapper;
import com.showcai.def.Constant;
import com.showcai.module.ApplyMaintain;
import com.showcai.module.BaseModule;
import com.showcai.module.Prizes;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.service.ApplyMaintainService;
import com.showcai.service.PrizesService;
import com.showcai.service.ServiceBase;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2018-9-5.
 */
@Service
public class PrizesServiceImpl extends ServiceBase implements PrizesService {

    @Autowired
    PrizesMapper prizesMapper;

    @Override
    public void modifyPrizes(Prizes prizes) throws MyRunTimeExcption {
        if(prizes.getTid()==null){
            prizes.setCreateTime(new Date());
            prizesMapper.insertPrizes(prizes);
            return;
        }
        prizesMapper.updatePrizesById(prizes);
    }

    @Override
    public List<Prizes> getPrizesListByActivityId(Integer activityId) throws MyRunTimeExcption {
        return prizesMapper.getPrizesListByActivityId(activityId);
    }

    @Override
    public PageInfo getPrizesList(Map<String, String> map) throws MyRunTimeExcption {
        String pageStr = map.get("page");
        String pageSizeStr = map.get("pageSize");
        Integer page = null;
        Integer pageSize = null;
        if (StringUtils.isNotBlank(pageStr) && StringUtils.isNumeric(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr) && StringUtils.isNumeric(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        if (null == page) {
            page = Constant.PAGENUM;
        }
        if (null == pageSize) {
            pageSize = Constant.PAGESIZENum;
        }

        PageHelper.startPage(page, pageSize);
        List<Prizes> prizesList = prizesMapper.selectPrizesList(map);
        PageInfo pageinfo = new PageInfo<>(prizesList);
        return pageinfo;
    }

    @Override
    public Prizes selectPrizesByTid(Long tid) throws MyRunTimeExcption {
        return prizesMapper.selectPrizesByTid(tid);
    }
}

